package cn.edu.sgu.www.mhxysy.controller.equipment;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.equipment.EquipmentDTO;
import cn.edu.sgu.www.mhxysy.dto.equipment.EquipmentForgeDTO;
import cn.edu.sgu.www.mhxysy.entity.equipment.Equipment;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.equipment.EquipmentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "装备管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/equipment", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class EquipmentController {

	private final EquipmentService equipmentService;

	@Autowired
	public EquipmentController(EquipmentService equipmentService) {
		this.equipmentService = equipmentService;
	}

	@ApiOperation("计算并修改装备评分")
	@RequestMapping(value = "/compute", method = RequestMethod.POST)
	public JsonResult<Void> compute(
			@ApiParam(name = "equipmentId", value = "装备ID", required = true)
			@NotBlank(message = CustomMessages.EQUIPMENT_ID_NOT_NULL) String equipmentId) {
		equipmentService.compute(equipmentId);

		return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
	}

	@ApiOperation("打造装备")
	@RequestMapping(value = "/forge", method = RequestMethod.POST)
	public JsonResult<Void> forge(@Validated EquipmentForgeDTO equipmentForgeDTO) {
		equipmentService.forge(equipmentForgeDTO);

		return JsonResult.success("打造成功");
	}

	@ApiOperation("添加装备")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated EquipmentDTO equipmentDTO) {
		equipmentService.insert(equipmentDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID批量删除装备")
	@RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
	public JsonResult<Void> deleteByIds(
			@ApiParam(name = "ids", value = "装备ID列表", required = true)
			@RequestParam("ids") List<String> ids) {
		equipmentService.deleteByIds(ids);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改装备信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated EquipmentDTO equipmentDTO) {
		equipmentService.updateById(equipmentDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询全部装备")
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<Equipment>> selectAll() {
		List<Equipment> list = equipmentService.selectAll();

		return JsonResult.success(null, list);
	}

	@ApiOperation("获取装备总评分")
	@RequestMapping(value = "/getTotalScore", method = RequestMethod.GET)
	public JsonResult<Integer> getTotalScore(
			@ApiParam(name = "equipmentId", value = "装备ID", required = true)
			@NotBlank(message = CustomMessages.EQUIPMENT_ID_NOT_NULL) String equipmentId) {
		int score = equipmentService.getTotalScore(equipmentId);

		return JsonResult.success(null, score);
	}

	@ApiOperation("通过角色ID查询装备列表")
	@RequestMapping(value = "/selectByRoleId", method = RequestMethod.GET)
	public JsonResult<List<Equipment>> selectByRoleId(
			@ApiParam(name = "roleId", value = "角色ID", required = false)
			@RequestParam(required = false) String roleId) {
		List<Equipment> list = equipmentService.selectByRoleId(roleId);

		return JsonResult.success(null, list);
	}

	@ApiOperation("通过ID查询装备信息")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<Equipment> selectById(
			@ApiParam(name = "id", value = "装备ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		Equipment equipment = equipmentService.selectById(id);

		return JsonResult.success(null, equipment);
	}

	@ApiOperation("分页查询装备列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<Equipment>> selectByPage(Pager<Equipment> pager) {
		Page<Equipment> page = equipmentService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

	@ApiOperation("通过装备类型ID查询装备列表")
	@RequestMapping(value = "/selectByCategoryId", method = RequestMethod.GET)
	public JsonResult<List<Equipment>> selectByCategoryId(
			@ApiParam(name = "categoryId", value = "装备类型ID", required = false)
			@RequestParam(required = false) Integer categoryId) {
		List<Equipment> list = equipmentService.selectByCategoryId(categoryId);

		return JsonResult.success(null, list);
	}

}